package luchbox;

import db_properties_path.*;
import java.sql.*;
import java.util.*;
import dbTool.*;

public class SHOP extends DBconnection2 {
	public SHOP(SetPath set) {
		super(set);
	}

	public SHOP() {
		super(new DefaultSetPath());
	}

	/** Auto Increasment */
	/** Primary Key */
	/** INT ; java.lang.Integer */
	private int shopid;
	/** VARCHAR ; java.lang.String */
	private String shop_number;
	/** VARCHAR ; java.lang.String */
	private String shop_name;
	/** VARCHAR ; java.lang.String */
	private String shop_phone;
	/** VARCHAR ; java.lang.String */
	private String shop_address;
	/** VARCHAR ; java.lang.String */
	private String data_creator;

	public int getShopid() {
		return shopid;
	}

	public void setShopid(int shopid) {
		this.shopid = shopid;
	}

	public String getShop_number() {
		return shop_number;
	}

	public void setShop_number(String shop_number) {
		this.shop_number = shop_number;
	}

	public String getShop_name() {
		return shop_name;
	}

	public void setShop_name(String shop_name) {
		this.shop_name = shop_name;
	}

	public String getShop_phone() {
		return shop_phone;
	}

	public void setShop_phone(String shop_phone) {
		this.shop_phone = shop_phone;
	}

	public String getShop_address() {
		return shop_address;
	}

	public void setShop_address(String shop_address) {
		this.shop_address = shop_address;
	}

	public String getData_creator() {
		return data_creator;
	}

	public void setData_creator(String data_creator) {
		this.data_creator = data_creator;
	}

	public interface fn {
		String TABLE_NAME = "SHOP";
		String PK = "shopid";
		String shopid = "shopid";
		String shop_number = "shop_number";
		String shop_name = "shop_name";
		String shop_phone = "shop_phone";
		String shop_address = "shop_address";
		String data_creator = "data_creator";
	}

	public String sql(String SQL) {
		return Utility.getSQL組合字(SQL, SHOP.fn.class);
	}

	public int clear() {
		String SQL = "delete from SHOP";
		Object parameter[] = {};
		return super.excuteUpdate(SQL, parameter);
	}

	public int delete(int shopid) {
		String SQL = "delete from SHOP where shopid=?";
		Object parameter[] = { shopid };
		return super.excuteUpdate(SQL, parameter);
	}

	public int update(String shop_number, String shop_name, String shop_phone,
			String shop_address, String data_creator, int shopid) {
		String SQL = "update SHOP set shop_number=?,shop_name=?,shop_phone=?,shop_address=?,data_creator=? where shopid=?";
		Object parameter[] = { shop_number, shop_name, shop_phone,
				shop_address, data_creator, shopid };
		return super.excuteUpdate(SQL, parameter);
	}

	public int insert(String shop_number, String shop_name, String shop_phone,
			String shop_address, String data_creator) {
		String SQL = "insert into SHOP(shop_number,shop_name,shop_phone,shop_address,data_creator) values(?,?,?,?,?)";
		Object parameter[] = { shop_number, shop_name, shop_phone,
				shop_address, data_creator };
		return super.excuteUpdate(SQL, parameter);
	}

	public String[][] select() {
		String SQL = "select * from SHOP";
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public SHOP[] select_Object() {
		String SQL = "select * from SHOP";
		Object parameter[] = {};
		String data[][] = super.select(SQL, parameter);
		SHOP obj[] = toObject(data);
		return obj;
	}

	public String[][] select(int shopid) {
		String SQL = "select * from SHOP where shopid=?";
		Object parameter[] = { shopid };
		return super.select(SQL, parameter);
	}

	public SHOP[] select_Object(int shopid) {
		String data[][] = this.select(shopid);
		SHOP obj[] = toObject(data);
		return obj;
	}

	public int selectCount() {
		String SQL = "select count(shopid) from SHOP";
		Object parameter[] = {};
		String d[][] = super.select(SQL, parameter);
		return Integer.parseInt(d[0][0]);
	}

	public String[][] selectPage(int pageSize, int pageIndex, int rowCount) {
		int desc = PaggingSQL.ASC;
		PaggingSQL ps = new dbTool.MySQL();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		String SQL = ps.getSelectPage(pageSize, pageIndex, TABLE_NAME, PK,
				desc, rowCount);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public String[][] selectPageOrderBy(int pageSize, int pageIndex,
			String sortName, int desc, int rowCount) {
		PaggingSQL ps = new dbTool.MySQL();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		String SQL = ps.getSelectPageOrderBy(pageSize, pageIndex, TABLE_NAME,
				PK, sortName, desc, rowCount);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	/**
	 * ����r where ���μg
	 * 
	 * @param pageSize
	 * @param pageIndex
	 * @param sortName
	 * @param desc
	 * @param rowCount
	 * @param whereCondition
	 *            = �ipk>0�j, ����r where ���μg
	 * @return
	 */
	public String[][] selectPageOrderBy(int pageSize, int pageIndex,
			String sortName, int desc, int rowCount, String whereCondition) {
		PaggingSQL ps = new dbTool.MySQL();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		// �d��,����r where ���μg, ex:�ipk>0�j
		String SQL = ps.getSelectPageOrderBy(pageSize, pageIndex, TABLE_NAME,
				PK, sortName, desc, rowCount, whereCondition);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public SHOP[] selectPage_Object(int pageSize, int pageIndex, int rowCount) {
		String data[][] = this.selectPage(pageSize, pageIndex, rowCount);
		SHOP obj[] = toObject(data);
		return obj;
	}

	/**
	 * ���ղέp����
	 * 
	 * @param fk_List
	 * @return
	 */
	public Map<Integer, Integer> getGroupCount(String fkName,
			LinkedList<Integer> fk_List) {
		String q = "";
		Object parameter[] = new Object[fk_List.size()];
		int i = 0;
		for (Integer no : fk_List) {
			q += "?,";
			parameter[i++] = no;
		}
		if (q.length() > 0) {
			q = q.substring(0, q.length() - 1);
		}
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		String SQL = "";
		SQL += "SELECT " + fkName + ", count(" + fkName + ") ";
		SQL += "FROM " + fn.TABLE_NAME + " where " + fkName + " in (" + q + ")";
		SQL += "group by " + fkName + ";";
		String data[][] = super.select(SQL, parameter);
		for (int j = 0; j < data.length; j++) {
			int key = Integer.parseInt(data[j][0]);
			int count = Integer.parseInt(data[j][1]);
			map.put(key, count);
		}
		return map;
	}

	/**
	 * Where PK in(?,?,?....)
	 * 
	 * @param idList
	 * @return
	 */
	public Map<Integer, SHOP> selectMap(LinkedList<Integer> idList) {
		String q = "";
		Object parameter[] = new Object[idList.size()];
		int i = 0;
		for (Integer no : idList) {
			q += "?,";
			parameter[i++] = no;
		}
		if (q.length() > 0) {
			q = q.substring(0, q.length() - 1);
		}
		HashMap<Integer, SHOP> map = new HashMap<Integer, SHOP>();

		String SQL = "select * from SHOP where shopid in (" + q + ")";
		String data[][] = super.select(SQL, parameter);
		SHOP[] obj = this.toObject(data);
		for (SHOP o : obj) {
			map.put(new Integer(o.shopid), o);
		}
		return map;
	}

	public SHOP[] toObject(String data[][]) {
		SHOP[] obj = new SHOP[data.length];
		for (int i = 0; i < obj.length; i++) {
			obj[i] = new SHOP(super.getSetting());
			obj[i].shopid = Integer.parseInt(data[i][0]);
			obj[i].shop_number = data[i][1];
			obj[i].shop_name = data[i][2];
			obj[i].shop_phone = data[i][3];
			obj[i].shop_address = data[i][4];
			obj[i].data_creator = data[i][5];
		}
		return obj;
	}
}